package com.framework.exception;

import com.framework.util.ResultData;

/**
 * 统一业务异常
 * @author LeeJun
 * @time 2017-8-10 13:29
 * @version 2.0.0
 */
public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = -5875371379845226068L;

    protected String msg;

    protected int code = -1;

    public BusinessException() {
        super();
    }

    public BusinessException(String message) {
        super(message);
        this.msg = message;
    }

    public BusinessException(String message, Object... objects) {
        super(message);
        this.msg = String.format(message, objects);
    }

    public BusinessException(Throwable cause) {
        super(cause);
    }

    public BusinessException(int code, Object... objects) {
        super(String.valueOf(code));
        this.code = code;
        String formatMsg = ResultData.getErrorMsg(code);
        this.msg = String.format(formatMsg, objects);
    }

    public static BusinessException error(int code, String msg) throws Exception {
        throw new BusinessException(code, msg);
    }

    public static BusinessException error(int code, String msg, Object... objects) throws Exception {
        throw new BusinessException(code, msg, objects);
    }

    public static BusinessException error(String msg) {
        throw new BusinessException(-1, msg);
    }

    public BusinessException(String message, Throwable cause) {
        super(message, cause);
    }

    public BusinessException(int code, String msg, Object... objects) {
        super(String.format(msg, objects));
        this.code = code;
        this.msg = String.format(msg, objects);
    }

    public int getCode() {
        return code;
    }

    @Override
    public String getLocalizedMessage() {
        return msg;
    }

    @Override
    public String getMessage() {
        return msg;
    }

    @Override
    public String toString() {
        return "BizException{" +
                "msg='" + msg + '\'' +
                ", code=" + code +
                "} " + super.toString();
    }
}



